import FindP from "./Astar";


const {ccclass, property} = cc._decorator;

@ccclass
export default class click extends cc.Component {

    @property(cc.Prefab)
    block:cc.Prefab=null
    // 地图当前位置
    x:number=null;
    y:number=null;
    start () {
        this.node.on(cc.Node.EventType.MOUSE_UP,this.click_end,this);
    }


    initposition(x:number,y:number){
        this.x=x;
        this.y=y;
    }

    // 计算路径并执行移动
    getpath_and_move(endx:number,endy:number){
        //停止移动
        let graphcis=cc.find('Canvas/draw').getComponent(cc.Graphics);
        graphcis.clear();
        if(cc.find('Canvas/player').getComponent('player').cur){
            cc.find('Canvas/player').getComponent('player').cur.stop();
            // 清空目的地
            cc.find('Canvas/player').getComponent('player').end_x=null;
            cc.find('Canvas/player').getComponent('player').end_y=null;
        }
            //计算路径
            let start_x=cc.find('Canvas/player').getComponent('player').x;
            let start_y=cc.find('Canvas/player').getComponent('player').y;
            let path:number[][]=[];
            // 如果终点和起点是一个点，则直接返回路径
            if(start_x==endx&&start_y==endy){
                path=[[start_x,start_y],[endx,endy]];
            }else{let findP=new FindP();
            path=findP.getpath([start_x,start_y],[endx,endy],cc.find('Canvas/map').getComponent('map').blocks);}
            //调用移动
            if(path.length!=0){
            cc.find('Canvas/player').getComponent('player').end_x=endx;
            cc.find('Canvas/player').getComponent('player').end_y=endy;
            cc.find('Canvas/player').getComponent('player').path=path;
            cc.find('Canvas/player').getComponent('player').walk_correct();}
    }

    // 地图点击事件
    click_end(event){
        if(event.getButton()==0){
            this.getpath_and_move(this.x,this.y);

        }else{
            // 创建障碍物
            cc.find('Canvas/map').getComponent('map').blocks.push([this.x,this.y]);
            let block=cc.instantiate(this.block);
            block.setParent(this.node.parent);
            block.getComponent('shitou').x=this.x;
            block.getComponent('shitou').y=this.y;
            block.setPosition(-375+(this.x-1)*50,150-(this.y-1)*50);
            //重新计算路径
            let index_block=this.find_index(cc.find('Canvas/player').getComponent('player').path,[this.x,this.y]);
            if(index_block!=-1){
                let index_player=this.find_index(cc.find('Canvas/player').getComponent('player').path,[cc.find('Canvas/player').getComponent('player').x,cc.find('Canvas/player').getComponent('player').y])
                if(index_player<index_block){
                    //重新计算路径
                    this.getpath_and_move(cc.find('Canvas/player').getComponent('player').end_x,cc.find('Canvas/player').getComponent('player').end_y)
                }
            }

        }


    }


    // 查询一位数组在二维数组的索引
    find_index(list2:number[][],list:number[]){
        for(let i=0;i<list2.length;i++){
            if(list2[i]+''==list+''){
                return i
            }
        }
        return -1


    }

}
